<script setup>
import {
    Avatar,
    Service,
    Management,
    Histogram,
    Promotion,
    UserFilled,
    Suitcase,
    User,
    Coin,
    SwitchButton,
    SuccessFilled,
    QuestionFilled,
    CaretBottom
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import {ref, onMounted} from 'vue'
import useUserStore from '@/stores/userStore'
import { getAdmin } from '@/axios/admin/adminReq'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
//条目被点击后调用的函数
const handleCommand = (command) => {
    //判断指令
    if(command === "logout"){
        //退出登录
        ElMessageBox.confirm(
        '你确认要退出吗?',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async() => {
            //退出登录
            //1.清空pinia中存储的token以及个人信息
            userStore.clear();
            //2.跳转到登录页面
            router.push('/login')
            ElMessage({
                type: 'success',
                message: '退出登录成功',
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '用户取消退出登录',
            })
        })
    }else{
        //路由
        router.push('/admin/user/'+command)
    }
}
// 管理员信息响应式对象
const adminInfo = ref({
    adminId: null,
    name: '管理员',
});

const userStore = useUserStore();
//调用函数获取用户信息
const getUserInfo = async () => {
    // 1. 从 userStore 获取管理员 ID
    const adminId = userStore.userId;
    const res = await getAdmin(adminId);
    //4.判断用户信息是否存在
    if(res){
        adminInfo.value = res;
    } else {
        ElMessage({
            type: 'error',
            message: '获取用户信息失败',
        });
    }
}

// 在组件挂载时获取用户信息
onMounted(() => {
    getUserInfo();
});
</script>

<template>
    <!-- element-plus 容器 -->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <!-- element-plus 菜单标签 -->
            <el-menu active-text-color="#ffd04b" 
            background-color="#232323"  
            text-color="#fff"
            router
            :default-active="$router.path"
            unique-opened>
                <el-menu-item index="/admin/manage/case/statistics">
                    <el-icon>
                        <Histogram />
                    </el-icon>
                    <span>数据统计</span>
                </el-menu-item>
                <el-menu-item index="/admin/manage/case/backlog">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>待办任务</span>
                </el-menu-item>
                <el-sub-menu index="case-management">
                    <template #title>
                        <el-icon>
                            <Promotion />
                        </el-icon>
                        <span>案件管理</span>
                    </template>
                    <el-menu-item index="/admin/manage/case/inject">
                        <el-icon>
                            <QuestionFilled />
                        </el-icon>
                        <span>拒绝的案件</span>
                    </el-menu-item>
                    <el-menu-item index="/admin/manage/case/unCompleted">
                        <el-icon>
                            <QuestionFilled />
                        </el-icon>
                        <span>调解中案件</span>
                    </el-menu-item>
                    <el-menu-item index="/admin/manage/case/completed">
                        <el-icon>
                            <SuccessFilled />
                        </el-icon>
                        <span>已完结案件</span>
                    </el-menu-item>
                </el-sub-menu>
                <el-sub-menu index="personnel-management">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>人员管理</span>
                    </template>
                    <el-menu-item index="/admin/manage/personnel/admins">
                    <el-icon>
                        <Avatar />
                    </el-icon>
                    <span>管理员管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/manage/personnel/mediators">
                    <el-icon>
                        <Service />
                    </el-icon>
                    <span>调解员管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/manage/personnel/applicants">
                    <el-icon>
                        <User />
                    </el-icon>
                    <span>申请人管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/manage/personnel/respondents">
                    <el-icon>
                        <User />
                    </el-icon>
                    <span>被申请人管理</span>
                </el-menu-item>
                <el-menu-item index="/admin/manage/personnel/assistants">
                    <el-icon>
                        <Service />
                    </el-icon>
                    <span>助手管理</span>
                </el-menu-item>
                </el-sub-menu>
                <el-sub-menu index="organization-management">
                    <template #title>
                        <el-icon>
                            <Suitcase />
                        </el-icon>
                        <span>组织管理</span>
                    </template>
                    <el-menu-item index="/admin/manage/organization/mediatorsOrg">
                        <el-icon>
                            <Service />
                        </el-icon>
                        <span>调解机构管理</span>
                    </el-menu-item>
                    <el-menu-item index="/admin/manage/organization/applicationIns">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>申请人机构管理</span>
                    </el-menu-item>
                </el-sub-menu>
                    <el-menu-item index="/admin/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>欢迎您，<strong>{{ adminInfo.name }}</strong></div>
                <!-- 下拉菜单 -->
                 <!-- command: 点击下拉菜单项后触发的事件，在事件函数上可以声明一个函数，接收条目对应的指令 -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        管理员:{{ adminInfo.name }}
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>法院调解平台 ©2024-2025 Created by 02</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('@/assets/logo.png') no-repeat center / 200px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>